﻿using System;
using System.Collections.Generic;

using System.Text;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.Core.Modelling.Primitives;

namespace ITAP.ShprossesBuilder.Manipulation.Tolerance
{
    public class ToleranceManager : IModelVisitor
    {
        private ModelContainer container;

        private bool enabled = true;

        public bool Enabled
        {
            get { return enabled; }
            set { enabled = value; }
        }

        #region IModelVisitor Members

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Model model)
        {
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Connector connector)
        {
            List<IComponent> componentsToDisconnect = new List<IComponent>();
            foreach (IComponent connectedComponent in connector.ConnectedComponents)
                if (connectedComponent is Shpross)
                    componentsToDisconnect.Add(connectedComponent);


            foreach (IComponent connectedComponent in componentsToDisconnect)
                    ConnectionProvider.Disconnect(connector, connectedComponent);                    
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Shpross shpross)
        {
            List<IComponent> componentsToDisconnect = new List<IComponent>();

            foreach (IComponent connectedComponent in shpross.ConnectedComponents)
            {
                // Отконнекчиваем приконнекченные сбоку компоненты
                EndType endType = shpross.IsConnectedToEnd(connectedComponent);
                if (endType != EndType.Start && endType != EndType.End)
                    componentsToDisconnect.Add(connectedComponent);
            }

            foreach (IComponent connectedComponent in componentsToDisconnect)
                ConnectionProvider.Disconnect(shpross, connectedComponent);
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Spacer spacer)
        {
            List<IComponent> componentsToDisconnect = new List<IComponent>();
        
            foreach (IComponent connectedComponent in spacer.ConnectedComponents)
            {
                if (connectedComponent is SpacerConnector)
                    componentsToDisconnect.Add(connectedComponent);
            }

            foreach (IComponent connectedComponent in componentsToDisconnect)
                ConnectionProvider.Disconnect(spacer, connectedComponent);

        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ShprossConnectorStub stub)
        {   
         
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ShprossConnector shprossConnector)
        {            
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.SpacerElement spacerElement)
        {
            //foreach (IComponent connectedComponent in spacerElement.ConnectedComponents)
            //{
            //    EndType endType = spacerElement.IsConnectedToEnd(connectedComponent);
            //    if(endType != EndType.Start && endType != EndType.End)
            //        ConnectionProvider
            //}
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.SpacerConnector spacerConnector)
        {
          //  throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ModelCell modelCell)
        {
          //  throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ModelContainer modelContainer)
        {
            container = modelContainer;
            List<IComponent> allInnerComponents = modelContainer.AllInnerComponents;
            foreach (IComponent innerComponent in allInnerComponents)
                innerComponent.ComponentsInTolerance.Clear();

            for (int i = 0; i < allInnerComponents.Count; i++)
            {
                if (this.enabled)
                {
                    for (int j = 0; j < allInnerComponents.Count; j++)
                    {
                        if (i != j)
                            allInnerComponents[i].AcceptVisitor(allInnerComponents[j]);
                    }
                }
                else
                {
                    allInnerComponents[i].AcceptVisitor(this);
                }
            }     
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(StickConnector stickConnector)
        {
            //throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(SnapOnCap snapOnCap)
        {
          //  throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(ShprossEndToEndConnector endToEndConnector)
        {
            endToEndConnector.DisconnectFromAll();
            //throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(ShprossChain shprossChain)
        {
     //       throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(SpacerContainer spacerContainer)
        {
            //throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(CrossConnector crossConnector)
        {
          //  throw new NotImplementedException();
        }

        #endregion

        public void Update()
        {
            this.Visit(container);
        }
    }
}
